package net.azure.app.util;

import net.azure.app.model.Story;
import net.azure.app.model.StoryDetails;
import net.azure.app.model.Risk;
import net.azure.app.model.Estimate;
import net.azure.app.GlobalConstants;

import java.util.HashMap;
import java.util.List;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

import au.com.bytecode.opencsv.CSVReader;

/**
 * Created by IntelliJ IDEA.
 * User: Venkatraman Kandaswamy : kvenkatraman@gmail.com
 * Date: 03-Jul-2008
 * Time: 06:57:09
 */
public class CSVLoader {
    public static HashMap<Integer, Story> loadCSV(File fileName) throws IOException {
        CSVReader reader = new CSVReader(new FileReader(fileName));
        //TODO: verify with default.csv if the order of columns are correct
        List myEntries = reader.readAll();
        HashMap<Integer,Story> stories = new HashMap<Integer, Story>();
        boolean header = true;
        for (Object myEntry : myEntries) {
            String[] columns = (String[]) myEntry;
            if(header){
                header = false;
                continue;
            }
            Integer key = Integer.parseInt(columns[0]);
            stories.put(key,makeStory(key,columns));
        }
        return stories;
    }

    private static Story makeStory(Integer key, String[] columns) {
        StoryDetails storyDetails = new StoryDetails(1, columns);
        Risk risk = new Risk(GlobalConstants.STORY_DETAILS_FIELD_COUNT+1,columns);
        Estimate estimate = new Estimate(GlobalConstants.STORY_DETAILS_FIELD_COUNT + GlobalConstants.RISK_FIELD_COUNT+1,columns);
        return new Story(key,storyDetails,risk,estimate);
    }
}
